package com.example.daxiguaforharmony.game;

import com.example.daxiguaforharmony.game.core.base.DisplayObjectContainer;
import com.example.daxiguaforharmony.game.core.game.SceneBase;
import com.example.daxiguaforharmony.game.core.renderers.Sprite;
import com.example.daxiguaforharmony.game.core.tween.Tween;
import com.example.daxiguaforharmony.game.core.utils.Callback;
import com.example.daxiguaforharmony.game.core.utils.MathUtil;
import ohos.agp.render.PixelMapHolder;

import java.util.ArrayList;
import java.util.List;

public class FruitsEffect {
    private static List<Sprite> effectPools = new ArrayList<Sprite>();

    public static void addEffectSprite(Sprite sprite) {
        sprite.visible = false;
        sprite.removeFromParent();
        effectPools.add(sprite);
    }

    public static Sprite getEffectSprite(String name) {
        Sprite sprite = null;
        for (int i = 0; i < effectPools.size(); i++) {
            Sprite s = effectPools.get(i);
            if (s.name != null && s.name.equals(name) && s.visible == false) {
                sprite = s;
                sprite.visible = true;
                break;
            }
        }
        if (sprite == null) {
            PixelMapHolder pixelMapHolder = AssetManager.getInstance().getFruits(name);
            sprite = new Sprite(pixelMapHolder);
            sprite.name = name;
        }
        return sprite;
    }

    public static void playEffect(int fruits, float px, float py, float width, SceneBase scene) {
        float scaleFactor = scene.getScaleFactor();
        DisplayObjectContainer container = scene.getDisplayContainer();
        float scale2 = scaleFactor + 0.3f;
        String fruitsName = "a" + fruits;
        for (int i = 0; i < 10; i++) {
            Sprite sprite = FruitsEffect.getEffectSprite(fruitsName);
            if (sprite == null) break;
            container.addChild(sprite);
            sprite.name = fruitsName;
            float a = MathUtil.random(0f, 359f);
            float r = MathUtil.random(0f, 30f) + width / 2f;
            float dx = (float)(Math.sin(a * Math.PI / 180) * r);
            float dy = (float)(Math.cos(a * Math.PI / 180) * r);
            float scale = MathUtil.random(0f, 0.5f) + width / (100f * scaleFactor);
            float t = MathUtil.random(0f, 0.5f);
            float rotation = MathUtil.random(360f, -360f);
            sprite.x = px;
            sprite.y = py;
            sprite.alpha = 1f;
            sprite.scaleX = scale;
            sprite.scaleY = scale;
            Tween.moveTo(sprite, t, sprite.x + dx, sprite.y + dy, null);
            Tween.scaleTo(sprite, t + 0.5f, scale2, scale2, new Callback.CompleteCallback() {
                @Override
                public void complete() {
                    Tween.fadeTo(sprite, 0.1f, 0f, new Callback.CompleteCallback() {
                        @Override
                        public void complete() {
                            FruitsEffect.addEffectSprite(sprite);
                        }
                    });
                }
            });
            Tween.rotateTo(sprite, t + 0.5f, rotation, null);
        }
        String fruitsName2 = "b" + fruits;
        for (int i = 0; i < 20; i++) {
            Sprite sprite = FruitsEffect.getEffectSprite(fruitsName2);
            if (sprite == null) break;
            container.addChild(sprite);
            sprite.name = fruitsName2;
            float a = MathUtil.random(0f, 359f);
            float r = MathUtil.random(0f, 30f) + width / 2f;
            float dx = (float)(Math.sin(a * Math.PI / 180) * r);
            float dy = (float)(Math.cos(a * Math.PI / 180) * r);
            float scale = 0.3f + width / (100f * scaleFactor);
            float t = MathUtil.random(0f, 0.5f);
            sprite.x = px;
            sprite.y = py;
            sprite.alpha = 1f;
            sprite.scaleX = scale;
            sprite.scaleY = scale;
            Tween.moveTo(sprite, t, sprite.x + dx, sprite.y + dy, null);
            Tween.scaleTo(sprite, t + 0.5f, scale2, scale2, new Callback.CompleteCallback() {
                @Override
                public void complete() {
                    Tween.fadeTo(sprite, 0.1f, 0f, new Callback.CompleteCallback() {
                        @Override
                        public void complete() {
                            FruitsEffect.addEffectSprite(sprite);
                        }
                    });
                }
            });
        }
        String fruitsName3 = "c" + fruits;
        Sprite sprite = FruitsEffect.getEffectSprite(fruitsName3);
        if (sprite != null) {
            container.addChild(sprite);
            sprite.name = fruitsName3;
            float scale = width / (150f * scaleFactor);
            float rotation = MathUtil.random(0f, 360f);
            sprite.x = px;
            sprite.y = py;
            sprite.alpha = 1f;
            sprite.scaleX = 0f;
            sprite.scaleY = 0f;
            sprite.rotation = rotation;
            Tween.scaleTo(sprite, 0.2f, scale, scale, null);
            Tween.fadeTo(sprite,1f, 0f, new Callback.CompleteCallback() {
                @Override
                public void complete() {
                    FruitsEffect.addEffectSprite(sprite);
                }
            });
        }
    }
}
